<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherCMS
 * @subpackage   Libs
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://julien.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class Typographer
{
	static function transforms($text)
	{
		// entities
		/*
		$from = array(
			// non-HTML curly quotes
			'/(?!<.*?)"([^"<>]+?)"(?![^<>]*?>)/',
			"/``(.+?)''/",
		);
		$text = preg_replace($from, '“\1”', $text);
		
		$from = array(
			// arrows (<->, ->, <-, <=>, =>, <=>)
			'&lt;-&gt;', '-&gt;', '&lt;-',
			'&lt;=&gt;', '=&gt;', '&lt;=',
			// french quotes (<< >>)
#			'&lt;&lt;', '&gt;&gt;',
			//
			'...',
			'(c)', '(tm)', '(r)'
		);
		$to = array(
			// arrows
			'&harr;', '&rarr;', '&larr;',
			'&hArr;', '&rArr;', '&lArr;',
			// french quotes
#			'«', '»',
			//
			'&#8230;',
			'&copy;', '&trade;', '&reg;'
		);
		$text = str_replace($from, $to, $text);
		*/
		
		// entities (outside HTML tags)
		$text = preg_replace(
			array(
				// curly quotes
				'/(?!<.*?)"([^"<>]+?)"(?![^<>]*?>)/', "/``(.+?)''/",
				// arrows
				'/(?!<.*?)&lt;-&gt;(?![^<>]*?>)/', '/(?!<.*?)-&gt;(?![^<>]*?>)/', '/(?!<.*?)&lt;-(?![^<>]*?>)/',
				'/(?!<.*?)&lt;=&gt;(?![^<>]*?>)/', '/(?!<.*?)=&gt;(?![^<>]*?>)/', '/(?!<.*?)&lt;=(?![^<>]*?>)/',
				// dashes
				'/(?!<.*?)---(?![^<>]*?>)/', '/(?!<.*?)--(?![^<>]*?>)/',
				// special chars
				'/(?!<.*?)\.\.\.(?![^<>]*?>)/', '/(?!<.*?)\(c\)(?![^<>]*?>)/', '/(?!<.*?)\(tm\)(?![^<>]*?>)/', '/(?!<.*?)\(r\)(?![^<>]*?>)/'
			),
			array(
				'“\1”', '“\1”',
				// arrows
				'&harr;', '&rarr;', '&larr;',
				'&hArr;', '&rArr;', '&lArr;',
				// dashes
				'&mdash;', '&ndash;',
				// special chars
				'&#8230;', '&copy;', '&trade;', '&reg;'
			),
			$text
		);
		
		// spaces
		$from = array(
			'/(\s+)(:|;|!|\?)/',
			'/(¿|¡)(\s+)/',
#			'/(«|&laquo;)\s+(.+?)\s+(»|&raquo;)/',
			'/«\s*(.+?)\s*»/',
			'/(\d)\s+(\d)/',
		);
		$to = array(
			'&nbsp;\2',
			'\1&nbsp;',
#			'&laquo;&nbsp;\2&nbsp;&raquo;',
			'&laquo;&nbsp;\1&nbsp;&raquo;',
			'\1&nbsp;\2',
		);
		$text = preg_replace($from, $to, $text);
		
		return $text;
	}
	
}
?>